package com.xyy.ec.qualification.utils;

import com.xyy.ec.qualification.enums.TaskEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class MergedUrl {
    public static List<Map<String, Object>> mergeUrl(List<Map<String, Object>> urls , Integer taskId) {
        List<Map<String, Object>> originalFormat = new ArrayList<>();
        Map<String, Object> mergedData = new HashMap<>();



        // 药检报告
        if (Objects.equals(taskId, TaskEnum.DRUG_REPORT.getTaskId())){
            for (Map<String, Object> entry : urls) {
                String key = entry.get("erp_code") + "|" + entry.get("batch_code");
                mergedData.merge(key, entry.get("image_url"), (existingValue, newValue) -> existingValue + "," + newValue);
            }
            for (Map.Entry<String, Object> entry : mergedData.entrySet()) {
                Map<String, Object> map = new HashMap<>();
                map.put("erp_code", entry.getKey().split("\\|")[0]);
                map.put("batch_code", entry.getKey().split("\\|")[1]);
                map.put("image_url", entry.getValue());
                originalFormat.add(map);
            }
        }
        // 药品首营资质
        if (Objects.equals(taskId, TaskEnum.FIRST_BATTALION_QUALIFICATION.getTaskId())){

            for (Map<String, Object> entry : urls) {
                String key = entry.get("erp_code").toString() ;
                mergedData.merge(key, entry.get("gmp_url"), (existingValue, newValue) -> existingValue + "," + newValue);
            }

            for (Map.Entry<String, Object> entry : mergedData.entrySet()) {
                Map<String, Object> map = new HashMap<>();
                map.put("erp_code", entry.getKey());
                map.put("gmp_url", entry.getValue());
                originalFormat.add(map);
            }

        }

        log.info("当前任务id：{},合并前数量：{}，合并后数量：{}", taskId,urls.size(), originalFormat.size());

        return originalFormat;
    }
}
